<?php
//登入注册API控制器
namespace app\api\controller;

//TP请求模块
use think\facade\Request;
//TP验证模块
use think\exception\ValidateException;
//TP验证模块
use think\facade\Validate;

//project模型
use app\api\model\Project as ProjectModel;
//user模型
use app\api\model\User as UserModel;
//user验证器
use app\api\validate\User as UserValidate;
//公共类
use app\api\common\Common;


class User
{   
    //获取项目管理列表
    public function getProjectList()
    {
        $uid = Common::getUid();
        $nowPage = Request::param('page');
        if (empty($nowPage)) {
            $nowPage = 1;
        }
        
        //查询数据
        $request = ProjectModel::where('uid', $uid)->field('id, name, price, image, show_state');

        $numPage = 12;//每页个数
        $lastPage = ceil($request->count()/$numPage);//末页页数
        
        $request = $request->limit(($nowPage-1)*$numPage,$numPage)->select();//输出分页数据

        $data = array(
            'nowPage' => (int)$nowPage,
            'lastPage' => (int)$lastPage,
            'data' => $request
        );

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create($data, '数据不存在', 204);
        } else {
            return Common::create($data, '数据请求成功', 200);
        }
    }

    //获取账户信息
    public function getAccount()
    {
        $uid = Common::getUid();
        $request = UserModel::where('uid', $uid)->findOrEmpty();
        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create($request, '数据不存在', 204);
        } else {
            return Common::create($request, '数据请求成功', 200);
        }
    }

    //修改账户密码
    public function postPassword()
    {
        //整理数据
        $uid = Common::getUid();
        $password = Request::param('password');
        $rpassword = Request::param('rpassword');
        $rpasswords = Request::param('rpasswords');
        
        //验证参数是否合法
        try {
            validate(UserValidate::class)->batch(true)
                                         ->scene('password')
                                         ->check([
                'password' => $rpassword,
                'rpassword' => $rpasswords
            ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $uservalidateerror = $e->getError();
            return Common::create($uservalidateerror, '修改失败', 400);
        }

        //获取模型请求对象
        $result = UserModel::where('uid', $uid)
                            ->where('password', sha1($password))
                            ->find();
        //验证账号是否正确
        if ($result) {
            //验证是否与原密码相同
            if($result['password'] == sha1($rpassword)){
                return Common::create(['与原密码相同'], '修改失败', 400);
            }
            //静态方法写入数据
            $result->password = sha1($rpassword);
            $user = $result->save();
            if($user){
                return Common::create([],'修改成功', 200);
            }
        }else{
            return Common::create([], '原密码错误', 401);
        }
        return Common::create([], '未知错误', 500);
    }
    
}
